package com.youhome.controller;

import com.youhome.model.BCounselor;
import com.youhome.model.BUser;
import com.youhome.response.Result;
import com.youhome.response.ResultCode;
import com.youhome.service.CounselorService;
import com.youhome.service.UserService;
import com.youhome.utils.JwtTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * Created by chen365 on 2018/09/17.
 */
@Api(tags = "帐号")
@RestController
@RequestMapping(value = "/accounts")
public class AccountController {

    @Autowired
    private UserService userService;

    @Autowired
    private CounselorService counselorService;

    @ApiOperation(value = "帐号详情", notes = "帐号详情", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "")
    public Object detail(HttpServletRequest request) {
        String token = request.getHeader("token");
        //System.err.println("detail-token:"+token);
        String usernameFromToken = new JwtTokenUtil().getUsernameFromToken(token);
        //System.err.println("detail-usernameFromToken:"+usernameFromToken);
        BUser bUser = userService.selectByPhone(usernameFromToken);
        if(bUser != null) {
            BCounselor bCounselor = counselorService.selectByUserId(bUser.getUserId());
            return new Result<>(ResultCode.SUCCESS, bCounselor);
        }
        return new Result<>(ResultCode.FAIL);
    }
}
